package com.szj360.job.executor.service.jobhandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.szj360.spider.CommonPageProcessor;
import com.szj360.spider.downloader.ImgDownloader;
import com.szj360.spider.pipeline.JobInfoDaoPipeline;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.HttpClientDownloader;


/**
 * 任务Handler的一个Demo（Bean模式）
 * 
 * 开发步骤：
 * 1、继承 “IJobHandler” ；
 * 2、装配到Spring，例如加 “@Service” 注解；
 * 3、加 “@JobHander” 注解，自定义属性name的值；name值在配置新任务是使用；
 * 
 * @author xuxueli 2015-12-19 19:43:36
 */
@JobHander(name="demoJobHandler")
@Service
public class DemoJobHandler extends IJobHandler {
	private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class);
	
	@Autowired
	private JobInfoDaoPipeline jobInfoDaoPipeline;
	
	@Override
	public JobHandleStatus execute(String... params) throws Exception {
		Spider.create(new CommonPageProcessor())
        //从"https://github.com/code4craft"开始抓
        .addUrl("http://www.cnbeta.com/")
        //开启1个线程抓取
        .thread(1)
        .addPipeline(this.jobInfoDaoPipeline)
        .setDownloader(new ImgDownloader())
//        .setDownloader(new SeleniumDownloader("/Users/yihua/Downloads/chromedriver"))
        //启动爬虫
        .run();
		
		
		return JobHandleStatus.SUCCESS;
	}
	
	
}
